package com.sdy.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * mysql> desc soundcode;
+----------+---------------+------+-----+---------+----------------+
| Field    | Type          | Null | Key | Default | Extra          |
+----------+---------------+------+-----+---------+----------------+
| id       | int(11)       | NO   | PRI | NULL    | auto_increment |
| value    | blob          | YES  |     | NULL    |                |
| title    | varchar(2000) | YES  |     | NULL    |                |
| index    | varchar(32)   | YES  |     | NULL    |                |
| modelId  | int(11)       | YES  |     | NULL    |                |
| language | varchar(32)   | YES  |     | NULL    |                |
+----------+---------------+------+-----+---------+----------------+
 * */
public class Soundcode extends Model<Soundcode> {

	private static final long serialVersionUID = -6035037890628037637L;
	
	public static final Soundcode dao = new Soundcode();
	
	public Map<String,Object> getCodes(int page,int rows){
		Map<String,Object> map = new HashMap<String,Object>();
		Page<Record> pages = Db.paginate(page, rows, "select * ", " from soundcode ");
		map.put("total", pages.getTotalRow());
		List<Record> list = pages.getList();
		for (Record record : list) {
			byte [] value = (byte[]) (record.get("value") == null?null:record.get("value"));
			record.set("value", value!=null? new String(value): null);
		}
		map.put("rows", list);
		map.put("totalPage",pages.getTotalPage());
		map.put("pageNumber",  pages.getPageNumber());
		return map;
	}
	
	public Soundcode getCodeById(int codeId){
		Soundcode code = this.findById(codeId);
		byte [] value = (byte[]) (code.get("value") == null?null:code.get("value"));
		code.set("value", value!=null? new String(value): null);
		return code;
	}
	
}
